package com.esri.core.internal.io.handler;

import com.esri.core.io.OnSelfSignedCertificateListener;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

/* loaded from: classes.dex */
public class f implements X509TrustManager {
    public static OnSelfSignedCertificateListener a = null;
    public static boolean b = false;
    public static final Map<X509Certificate, Boolean> c = new ConcurrentHashMap();
    private X509TrustManager d;

    public f(KeyStore keyStore) throws NoSuchAlgorithmException, KeyStoreException {
        this.d = null;
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        if (trustManagers.length == 0) {
            throw new NoSuchAlgorithmException("no trust manager found");
        }
        this.d = (X509TrustManager) trustManagers[0];
    }

    @Override // javax.net.ssl.X509TrustManager
    public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
        this.d.checkClientTrusted(x509CertificateArr, str);
    }

    @Override // javax.net.ssl.X509TrustManager
    public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
        if (b && x509CertificateArr != null) {
            for (X509Certificate x509Certificate : x509CertificateArr) {
                if (x509Certificate != null) {
                    x509Certificate.checkValidity();
                }
            }
            return;
        }
        try {
            this.d.checkServerTrusted(x509CertificateArr, str);
        } catch (CertificateException e) {
            if (a != null && x509CertificateArr != null && x509CertificateArr.length > 0) {
                Map<X509Certificate, Boolean> map = c;
                Boolean bool = map.isEmpty() ? null : map.get(x509CertificateArr[0]);
                if (bool == null) {
                    bool = Boolean.valueOf(a.checkServerTrusted(x509CertificateArr, str));
                    map.put(x509CertificateArr[0], bool);
                }
                if (bool != null && bool.booleanValue()) {
                    return;
                }
            }
            throw e;
        }
    }

    @Override // javax.net.ssl.X509TrustManager
    public X509Certificate[] getAcceptedIssuers() {
        return this.d.getAcceptedIssuers();
    }
}
